package com.starblink.post.contentdeleted.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.ext.commContext
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.network.config.PostConfig
import com.starblink.android.basic.util.ImageUtils
import com.starblink.rocketreserver.FetchPostsQuery
import com.starblink.rocketreserver.fragment.PostF
import com.starblink.rocketreserver.type.PostRecommendQueryParam
import kotlinx.coroutines.launch

class ContentDeletedVM : BaseViewModel() {

    var mList: MutableLiveData<ArrayList<PostF>> = MutableLiveData(arrayListOf())
    val pageSize = 12
    var pageNo = 0

    fun getRecommend(initRefresh: Boolean = false) {
        if (initRefresh) {
        }
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchPostsQuery(
                    postParams = PostRecommendQueryParam(
                        1,
                        relatedPostId = Optional.present(""),
                        PostConfig.postRecommendPage,
                        pageSize,
                        advertiseId = Optional.Absent,
                        clientTime = Optional.present(System.currentTimeMillis().toString()),
                        isReported = Optional.present(true),
                    ), isPostDetail = false
                )

            )
//            loadSuccess()
            if (response?.data != null && response.data!!.fetchRecommendPost != null) {
                var list1 = response.data!!.fetchRecommendPost
                var list = arrayListOf<PostF>()
                list1!!.forEach { e ->
                    list.add(e.postF)
                    var imgUrl = e.postF.mainPic
                    ImageUtils.preloadImage(commContext(), imgUrl)
                }
                PostConfig.postRecommendPage += 1
                if (initRefresh && mList.value!!.size > 1) {
                    mList.value!!.clear()
                }
                mList.value?.addAll(list)
                mList.value = mList.value
            } else {
                if (initRefresh) {
                    loadNoData()
                }
            }
        }
    }

}